package cn.iocoder.yudao.module.bpm.controller.admin.oaleavapplysub;

import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import jakarta.validation.constraints.*;
import jakarta.validation.*;
import jakarta.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.bpm.controller.admin.oaleavapplysub.vo.*;
import cn.iocoder.yudao.module.bpm.dal.dataobject.oaleavapplysub.OaLeavApplySubDO;
import cn.iocoder.yudao.module.bpm.service.oaleavapplysub.OaLeavApplySubService;

@Tag(name = "管理后台 - 请假申请")
@RestController
@RequestMapping("/bpm/oa-leav-apply-sub")
@Validated
public class OaLeavApplySubController {

    @Resource
    private OaLeavApplySubService oaLeavApplySubService;

    @PostMapping("/create")
    @Operation(summary = "创建请假申请")
    public CommonResult<Long> createOaLeavApplySub(@Valid @RequestBody OaLeavApplySubSaveReqVO createReqVO) {
        return success(oaLeavApplySubService.createOaLeavApplySub(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新请假申请")
    public CommonResult<Boolean> updateOaLeavApplySub(@Valid @RequestBody OaLeavApplySubSaveReqVO updateReqVO) {
        oaLeavApplySubService.updateOaLeavApplySub(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除请假申请")
    @Parameter(name = "id", description = "编号", required = true)
    public CommonResult<Boolean> deleteOaLeavApplySub(@RequestParam("id") Long id) {
        oaLeavApplySubService.deleteOaLeavApplySub(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得请假申请")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    public CommonResult<OaLeavApplySubRespVO> getOaLeavApplySub(@RequestParam("id") Long id) {
        OaLeavApplySubDO oaLeavApplySub = oaLeavApplySubService.getOaLeavApplySub(id);
        return success(BeanUtils.toBean(oaLeavApplySub, OaLeavApplySubRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得请假申请分页")
    public CommonResult<PageResult<OaLeavApplySubRespVO>> getOaLeavApplySubPage(@Valid OaLeavApplySubPageReqVO pageReqVO) {
        PageResult<OaLeavApplySubDO> pageResult = oaLeavApplySubService.getOaLeavApplySubPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, OaLeavApplySubRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出请假申请 Excel")
    @ApiAccessLog(operateType = EXPORT)
    public void exportOaLeavApplySubExcel(@Valid OaLeavApplySubPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<OaLeavApplySubDO> list = oaLeavApplySubService.getOaLeavApplySubPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "请假申请.xls", "数据", OaLeavApplySubRespVO.class,
                        BeanUtils.toBean(list, OaLeavApplySubRespVO.class));
    }

}
